package com.yelink.metrics.core.domain;


import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.Date;

/**
 * 前后通讯数据格式
 *
 * @author shuang
 */
@Data
@NoArgsConstructor
@Slf4j
public class ResponseData implements Serializable {
    private static final long serialVersionUID = 1L;
    public static final String SUCCESS_CODE = "200";
    public static final String IOT_SUCCESS_CODE = "0";
    public static final String JZ_SUCCESS_CODE = "0";
    public static final String FAIL_CODE = "300";

    /**
     * 状态码
     * 200：操作成 300：操作失败 400：未登录
     */
    private String code;
    /**
     * 状态码：0 表示失败* 、 1表示成功
     */
    private Integer status;
    /**
     * 消息提示
     * 默认操作成功
     */
    private String message;
    /**
     * 当前服务器时间
     * 格式yyyy-MM-dd HH:mm:ss
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date time = new Date();
    /**
     * 业务数据
     */
    private Object data;


    /**
     * 构造操作成功数据包   *
     *
     * @param data    业务数据
     * @param message 提示消息
     * @return
     */
    public static ResponseData success(Object data, String message) {
        ResponseData responseData = new ResponseData();
        responseData.setData(data);
        responseData.setCode(SUCCESS_CODE);
        responseData.setMessage(message);
        return responseData;
    }

    /**
     * 构造操作成功数据包
     *
     * @param data
     * @return
     */
    public static ResponseData success(Object data) {
        ResponseData responseData = new ResponseData();
        responseData.setData(data);
        responseData.setCode(SUCCESS_CODE);
        responseData.setMessage("操作成功");
        return responseData;
    }

    /**
     * 构造操作成功数据包
     */
    public static ResponseData success(String message) {
        ResponseData responseData = new ResponseData();
        responseData.setData(message);
        responseData.setCode(SUCCESS_CODE);
        responseData.setMessage(message);
        return responseData;
    }

    /**
     * 构造操作成功数据包
     *
     * @return
     */
    public static ResponseData success() {
        ResponseData responseData = new ResponseData();
        responseData.setData(null);
        responseData.setCode(SUCCESS_CODE);
        responseData.setMessage("操作成功");
        return responseData;
    }


    /**
     * 构造操作失败数据包
     *
     * @return
     */
    public static ResponseData fail(String message) {
        ResponseData responseData = new ResponseData();
        responseData.setData(null);
        responseData.setCode(FAIL_CODE);
        responseData.setMessage(message);
        return responseData;
    }

    /**
     * 构造操作失败数据包(可指定状态码)
     *
     * @return
     */
    public static ResponseData failCode(String message, String code) {
        ResponseData responseData = new ResponseData();
        responseData.setData(null);
        responseData.setCode(code);
        responseData.setMessage(message);
        return responseData;
    }

    /**
     * 构造操作失败数据包
     *
     * @return
     */
    public static ResponseData fail() {
        ResponseData responseData = new ResponseData();
        responseData.setData(null);
        responseData.setCode(FAIL_CODE);
        responseData.setMessage("操作失败");
        return responseData;
    }



}
